
package org.openbravo.erpWindows.ManualSettlement;

import org.openbravo.erpCommon.utility.*;
import org.openbravo.erpCommon.reference.*;
import org.openbravo.erpCommon.ad_actionButton.*;
import org.openbravo.data.FieldProvider;
import org.openbravo.utils.FormatUtilities;
import org.openbravo.erpCommon.info.LocationSearchData;
import org.openbravo.base.secureApp.HttpSecureAppServlet;
import org.openbravo.base.secureApp.VariablesSecureApp;
import org.openbravo.erpCommon.businessUtility.WindowTabs;
import org.openbravo.erpCommon.businessUtility.WindowTabsData;
import org.openbravo.utils.Replace;
import org.openbravo.xmlEngine.XmlDocument;
import org.openbravo.data.UtilSql;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// imports para transacciones
import org.openbravo.database.ConnectionProvider;
import java.sql.Connection;

import org.openbravo.erpCommon.utility.DateTimeData;

public class ManualSettlement extends HttpSecureAppServlet {
  protected static final String windowId = "800006";
  protected static final String tabId = "800029";
  protected static final String tableLevel = "3";
  protected static final String defaultTabView = "RELATION";
  protected static final double SUBTABS_COL_SIZE = 15;
  protected TableSQLData tableSQL = null;

  public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
    VariablesSecureApp vars = new VariablesSecureApp(request);

    if (!Utility.hasAccess(this, vars, tableLevel, Utility.getContext(this, vars, "#AD_Client_ID", windowId), Utility.getContext(this, vars, "#AD_Org_ID", windowId), windowId, tabId)) {
      bdError(response, "AccessTableNoView", vars.getLanguage());
      return;
    }

    try {
      tableSQL = new TableSQLData(vars, this, tabId, Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId));
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    String strOrderBy = vars.getSessionValue(tabId + "|orderby");
    if (!strOrderBy.equals("")) {
      vars.setSessionValue(tabId + "|newOrder", "1");
    }

    if (vars.commandIn("DEFAULT")) {

      String strC_Settlement_ID = vars.getGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID", "");
      

      String strView = vars.getSessionValue(tabId + "|ManualSettlement.view");
      if (strView.equals("")) {
        strView = defaultTabView;

        if (strView.equals("EDIT")) {
          if (strC_Settlement_ID.equals("")) strC_Settlement_ID = firstElement(vars);
          if (strC_Settlement_ID.equals("")) strView = "RELATION";
        }
      }
      if (strView.equals("EDIT")) 

        printPageEdit(response, request, vars, false, strC_Settlement_ID);

      else printPageDataSheet(response, vars, strC_Settlement_ID);
    } else if (vars.commandIn("DIRECT")) {
      String strC_Settlement_ID = vars.getStringParameter("inpDirectKey");
      if (strC_Settlement_ID.equals("")) strC_Settlement_ID = vars.getRequiredGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID");
      else vars.setSessionValue(windowId + "|C_Settlement_ID", strC_Settlement_ID);
      
      vars.setSessionValue(tabId + "|ManualSettlement.view", "EDIT");

      printPageEdit(response, request, vars, false, strC_Settlement_ID);

    } else if (vars.commandIn("TAB")) {


      String strView = vars.getSessionValue(tabId + "|ManualSettlement.view");
      String strC_Settlement_ID = "";
      if (strView.equals("")) {
        strView = defaultTabView;
        if (strView.equals("EDIT")) {
          strC_Settlement_ID = firstElement(vars);
          if (strC_Settlement_ID.equals("")) strView = "RELATION";
        }
      }
      if (strView.equals("EDIT")) {

        if (strC_Settlement_ID.equals("")) strC_Settlement_ID = firstElement(vars);
        printPageEdit(response, request, vars, false, strC_Settlement_ID);

      } else printPageDataSheet(response, vars, "");
    } else if (vars.commandIn("SEARCH")) {
      String strParamDocumentNo = vars.getRequestGlobalVariable("inpParamDocumentNo", tabId + "|paramDocumentNo");
      String strParamDateTrx = vars.getRequestGlobalVariable("inpParamDateTrx", tabId + "|paramDateTrx");
      String strParamSettlementtype = vars.getRequestGlobalVariable("inpParamSettlementtype", tabId + "|paramSettlementtype");
      String strParamDateTrx_f = vars.getRequestGlobalVariable("inpParamDateTrx_f", tabId + "|paramDateTrx_f");

      //vars.setSessionValue(tabId + "|paramSessionDate", Utility.getContext(this, vars, "#Date", windowId));
      //vars.setSessionValue(tabId + "|paramSessionDate", Utility.getTransactionalDate(this, vars, windowId));
      
      
      vars.removeSessionValue(windowId + "|C_Settlement_ID");
      String strC_Settlement_ID="";

      String strView = vars.getSessionValue(tabId + "|ManualSettlement.view");
      if (strView.equals("")) strView=defaultTabView;

      if (strView.equals("EDIT")) {
        strC_Settlement_ID = firstElement(vars);
        if (strC_Settlement_ID.equals("")) strView = "RELATION";
      }

      if (strView.equals("EDIT")) 

        printPageEdit(response, request, vars, false, strC_Settlement_ID);

      else printPageDataSheet(response, vars, strC_Settlement_ID);
    } else if (vars.commandIn("RELATION")) {
      

      String strC_Settlement_ID = vars.getGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID", "");
      vars.setSessionValue(tabId + "|ManualSettlement.view", "RELATION");
      printPageDataSheet(response, vars, strC_Settlement_ID);
    } else if (vars.commandIn("NEW")) {


      printPageEdit(response, request, vars, true, "");

    } else if (vars.commandIn("EDIT")) {

      String strC_Settlement_ID = vars.getGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID", "");
      vars.setSessionValue(tabId + "|ManualSettlement.view", "EDIT");

      setHistoryCommand(request, "EDIT");
      printPageEdit(response, request, vars, false, strC_Settlement_ID);

    } else if (vars.commandIn("NEXT")) {

      String strC_Settlement_ID = vars.getRequiredStringParameter("inpcSettlementId");
      
      String strNext = nextElement(vars, strC_Settlement_ID);

      printPageEdit(response, request, vars, false, strNext);
    } else if (vars.commandIn("PREVIOUS")) {

      String strC_Settlement_ID = vars.getRequiredStringParameter("inpcSettlementId");
      
      String strPrevious = previousElement(vars, strC_Settlement_ID);

      printPageEdit(response, request, vars, false, strPrevious);
    } else if (vars.commandIn("FIRST_RELATION")) {

      vars.setSessionValue(tabId + "|ManualSettlement.initRecordNumber", "0");
      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("PREVIOUS_RELATION")) {

      String strInitRecord = vars.getSessionValue(tabId + "|ManualSettlement.initRecordNumber");
      String strRecordRange = Utility.getContext(this, vars, "#RecordRange", windowId);
      int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
      if (strInitRecord.equals("") || strInitRecord.equals("0")) {
        vars.setSessionValue(tabId + "|ManualSettlement.initRecordNumber", "0");
      } else {
        int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
        initRecord -= intRecordRange;
        strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
        vars.setSessionValue(tabId + "|ManualSettlement.initRecordNumber", strInitRecord);
      }
      vars.removeSessionValue(windowId + "|C_Settlement_ID");

      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("NEXT_RELATION")) {

      String strInitRecord = vars.getSessionValue(tabId + "|ManualSettlement.initRecordNumber");
      String strRecordRange = Utility.getContext(this, vars, "#RecordRange", windowId);
      int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
      int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
      if (initRecord==0) initRecord=1;
      initRecord += intRecordRange;
      strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
      vars.setSessionValue(tabId + "|ManualSettlement.initRecordNumber", strInitRecord);
      vars.removeSessionValue(windowId + "|C_Settlement_ID");

      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("FIRST")) {

      String strC_Settlement_ID = vars.getRequiredStringParameter("inpcSettlementId");
      
      String strFirst = firstElement(vars);

      printPageEdit(response, request, vars, false, strFirst);
    } else if (vars.commandIn("LAST_RELATION")) {

      String strLast = lastElement(vars);
      printPageDataSheet(response, vars, strLast);
    } else if (vars.commandIn("LAST")) {

      String strC_Settlement_ID = vars.getRequiredStringParameter("inpcSettlementId");
      
      String strLast = lastElement(vars);

      printPageEdit(response, request, vars, false, strLast);
    } else if (vars.commandIn("SAVE_NEW_RELATION", "SAVE_NEW_NEW", "SAVE_NEW_EDIT")) {

      OBError myError = null;
      ManualSettlementData data = null;
      int total = 0;
      try {
        data = getEditVariables(vars);
        String strSequence = SequenceIdData.getSequence(this, "C_Settlement", vars.getClient());
        log4j.info("Sequence: " + strSequence);
        data.cSettlementId = strSequence;
        total = data.insert(this);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      if (myError!=null) {
        if (data!=null) {
          data.cSettlementId = "";
          vars.setEditionData(tabId, data);
        }
        response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
      } else {
        if (myError==null) {
          myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsInserted");
          myError.setMessage(total + " " + myError.getMessage());
          vars.setMessage(tabId, myError);
        }
        vars.setSessionValue(windowId + "|C_Settlement_ID", data.cSettlementId);
        if (vars.commandIn("SAVE_NEW_NEW")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
        else if (vars.commandIn("SAVE_NEW_EDIT")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        else response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
      }
    } else if (vars.commandIn("SAVE_EDIT_RELATION", "SAVE_EDIT_NEW", "SAVE_EDIT_EDIT", "SAVE_EDIT_NEXT")) {

      String strC_Settlement_ID = vars.getRequiredGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID");
      int total = 0;
      ManualSettlementData data = null;
      OBError myError = null;
      try {
        data = getEditVariables(vars);
        total = data.update(this);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      if (myError!=null) {
        if (data!=null) {
          {
            //BUTTON TEXT FILLING
            data.processedBtn = ActionButtonDefaultData.getText(this, vars.getLanguage(), "800024", data.getField("Processed"));
          }{
            //BUTTON TEXT FILLING
            data.postedBtn = ActionButtonDefaultData.getText(this, vars.getLanguage(), "234", data.getField("Posted"));
          }
          vars.setEditionData(tabId, data);
        }
        response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
      } else {
        if (myError==null) {
          myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsUpdated");
          myError.setMessage(total + " " + myError.getMessage());
          vars.setMessage(tabId, myError);
        }
        if (vars.commandIn("SAVE_EDIT_NEW")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
        else if (vars.commandIn("SAVE_EDIT_EDIT")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        else if (vars.commandIn("SAVE_EDIT_NEXT")) {
          String strNext = nextElement(vars, strC_Settlement_ID);
          vars.setSessionValue(windowId + "|C_Settlement_ID", strNext);
          response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        } else response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
      }
/*    } else if (vars.commandIn("DELETE_RELATION")) {

      String strC_Settlement_ID = vars.getRequiredInStringParameter("inpcSettlementId");
      String message = deleteRelation(vars, strC_Settlement_ID);
      if (!message.equals("")) {
        bdError(response, message, vars.getLanguage());
      } else {
        vars.removeSessionValue(windowId + "|cSettlementId");
        vars.setSessionValue(tabId + "|ManualSettlement.view", "RELATION");
        response.sendRedirect(strDireccion + request.getServletPath());
      }*/
    } else if (vars.commandIn("DELETE")) {

      String strC_Settlement_ID = vars.getRequiredStringParameter("inpcSettlementId");
      //ManualSettlementData data = getEditVariables(vars);
      int total = 0;
      OBError myError = null;
      try {
        total = ManualSettlementData.delete(this, strC_Settlement_ID);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      vars.removeSessionValue(windowId + "|cSettlementId");
      vars.setSessionValue(tabId + "|ManualSettlement.view", "RELATION");
      if (myError==null) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsDeleted");
        myError.setMessage(total + " " + myError.getMessage());
        vars.setMessage(tabId, myError);
      }
      response.sendRedirect(strDireccion + request.getServletPath());
    } else if (vars.commandIn("BUTTONProcessed800025")) {
        String strC_Settlement_ID = vars.getGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID", "");
        String strprocessed = vars.getStringParameter("inpprocessed");
        String strProcessing = vars.getStringParameter("inpprocessing", "Y");
        
        printPageButtonProcessed800025(response, vars, strC_Settlement_ID, strprocessed, strProcessing);

    } else if (vars.commandIn("SAVE_BUTTONProcessed800025")) {
        String strC_Settlement_ID = vars.getGlobalVariable("inpKey", windowId + "|C_Settlement_ID", "");
        String strprocessed = vars.getStringParameter("inpprocessed");
        
        String strProcessing = vars.getStringParameter("inpprocessing");
        OBError myMessage = null;
        try {
          String pinstance = SequenceIdData.getSequence(this, "AD_PInstance", vars.getClient());
          PInstanceProcessData.insertPInstance(this, pinstance, "800025", (("C_Settlement_ID".equalsIgnoreCase("AD_Language"))?"0":strC_Settlement_ID), strProcessing, vars.getUser(), vars.getClient(), vars.getOrg());
          
          
          ActionButtonData.process800025(this, pinstance);

          PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(this, pinstance);
          String messageResult="";
          myMessage = Utility.getProcessInstanceMessage(this, vars, pinstanceData);
        } catch (ServletException ex) {
          myMessage = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
          if (!myMessage.isConnectionAvailable()) {
            bdErrorConnection(response);
            return;
          } else vars.setMessage(tabId, myMessage);
        }
        if (myMessage!=null) {
          if (log4j.isDebugEnabled()) log4j.debug(myMessage.getMessage());
          vars.setMessage(tabId, myMessage);
        }
        printPageClosePopUp(response, vars);


    } else if (vars.commandIn("BUTTONPosted")) {
        String strC_Settlement_ID = vars.getGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID", "");
        String strTableId = "800019";
        String strPosted = vars.getStringParameter("inpposted");
        String strProcessId = "";
        log4j.debug("Loading Posted button in table: " + strTableId);
        vars.setSessionValue("Posted|key", strC_Settlement_ID);
        vars.setSessionValue("Posted|tableId", strTableId);
        vars.setSessionValue("Posted|tabId", tabId);
        vars.setSessionValue("Posted|posted", strPosted);
        vars.setSessionValue("Posted|processId", strProcessId);
        vars.setSessionValue("Posted|path", strDireccion + request.getServletPath());
        vars.setSessionValue("Posted|windowId", windowId);
        vars.setSessionValue("Posted|tabName", "ManualSettlement");
        response.sendRedirect(strDireccion + "/ad_actionButton/Posted.html");



    } else if (vars.getCommand().toUpperCase().startsWith("BUTTON") || vars.getCommand().toUpperCase().startsWith("SAVE_BUTTON")) {
      pageErrorPopUp(response);
    } else pageError(response);
  }
/*
  String deleteRelation(VariablesSecureApp vars, String strC_Settlement_ID) throws IOException, ServletException {
    log4j.debug("Deleting records");
    Connection conn = this.getTransactionConnection();
    try {
      if (strC_Settlement_ID.startsWith("(")) strC_Settlement_ID = strC_Settlement_ID.substring(1, strC_Settlement_ID.length()-1);
      if (!strC_Settlement_ID.equals("")) {
        strC_Settlement_ID = Replace.replace(strC_Settlement_ID, "'", "");
        StringTokenizer st = new StringTokenizer(strC_Settlement_ID, ",", false);
        while (st.hasMoreTokens()) {
          String strKey = st.nextToken();
          if (ManualSettlementData.deleteTransactional(conn, this, strKey)==0) {
            releaseRollbackConnection(conn);
            log4j.warn("deleteRelation - key :" + strKey + " - 0 records deleted");
          }
        }
      }
      releaseCommitConnection(conn);
    } catch (ServletException e) {
      releaseRollbackConnection(conn);
      e.printStackTrace();
      log4j.error("Rollback in transaction");
      return "ProcessRunError";
    }
    return "";
  }
*/
  ManualSettlementData getEditVariables(VariablesSecureApp vars) throws IOException,ServletException {
    ManualSettlementData data = new ManualSettlementData();
    data.adClientId = vars.getRequiredGlobalVariable("inpadClientId", windowId + "|AD_Client_ID");    data.adClientIdr = vars.getStringParameter("inpadClientId_R");    data.adOrgId = vars.getRequiredGlobalVariable("inpadOrgId", windowId + "|AD_Org_ID");    data.adOrgIdr = vars.getStringParameter("inpadOrgId_R");    data.documentno = vars.getRequiredStringParameter("inpdocumentno");    data.cCurrencyId = vars.getRequiredStringParameter("inpcCurrencyId");    data.cCurrencyIdr = vars.getStringParameter("inpcCurrencyId_R");    data.cDoctypeId = vars.getRequiredStringParameter("inpcDoctypeId");    data.cDoctypeIdr = vars.getStringParameter("inpcDoctypeId_R");    data.settlementtype = vars.getStringParameter("inpsettlementtype");    data.settlementtyper = vars.getStringParameter("inpsettlementtype_R");    data.datetrx = vars.getRequiredStringParameter("inpdatetrx");    data.dateacct = vars.getStringParameter("inpdateacct");    data.description = vars.getStringParameter("inpdescription");    data.istemplate = vars.getStringParameter("inpistemplate", "N");    data.cProjectId = vars.getStringParameter("inpcProjectId");    data.cProjectIdr = vars.getStringParameter("inpcProjectId_R");    data.cCampaignId = vars.getStringParameter("inpcCampaignId");    data.cCampaignIdr = vars.getStringParameter("inpcCampaignId_R");    data.cActivityId = vars.getStringParameter("inpcActivityId");    data.cActivityIdr = vars.getStringParameter("inpcActivityId_R");    data.generatedamt = vars.getStringParameter("inpgeneratedamt");    data.user1Id = vars.getStringParameter("inpuser1Id");    data.user1Idr = vars.getStringParameter("inpuser1Id_R");    data.user2Id = vars.getStringParameter("inpuser2Id");    data.user2Idr = vars.getStringParameter("inpuser2Id_R");    data.copyfrom = vars.getRequiredStringParameter("inpcopyfrom");    data.processed = vars.getRequiredStringParameter("inpprocessed");    data.posted = vars.getRequiredStringParameter("inpposted");    data.isactive = vars.getStringParameter("inpisactive", "N");    data.processing = vars.getStringParameter("inpprocessing", "N");    data.settlementopen = vars.getStringParameter("inpsettlementopen");    data.cancelednotchargeamt = vars.getStringParameter("inpcancelednotchargeamt");    data.cSettlementId = vars.getRequestGlobalVariable("inpcSettlementId", windowId + "|C_Settlement_ID");    data.chargedamt = vars.getStringParameter("inpchargedamt");    data.createfile = vars.getRequiredStringParameter("inpcreatefile");    data.createfrom = vars.getStringParameter("inpcreatefrom");
      data.createdby = vars.getUser();
      data.updatedby = vars.getUser();


          vars.getGlobalVariable("inptypesettlement", windowId + "|TYPESETTLEMENT", "");
          vars.getGlobalVariable("inpdoctypeSettlement", windowId + "|DOCTYPE_SETTLEMENT", "");
    
         if (data.documentno.startsWith("<")) data.documentno = Utility.getDocumentNo(this, vars, windowId, "C_Settlement", "", data.cDoctypeId, false, true);

    

    return data;
  }

   ManualSettlementData[] getRelationData(ManualSettlementData[] data) {
    if (data!=null) {
      for (int i=0;i<data.length;i++) {        data[i].adClientId = FormatUtilities.truncate(data[i].adClientId, 14);        data[i].adOrgId = FormatUtilities.truncate(data[i].adOrgId, 14);        data[i].documentno = FormatUtilities.truncate(data[i].documentno, 10);        data[i].cCurrencyId = FormatUtilities.truncate(data[i].cCurrencyId, 14);        data[i].cDoctypeId = FormatUtilities.truncate(data[i].cDoctypeId, 40);        data[i].settlementtype = FormatUtilities.truncate(data[i].settlementtype, 10);        data[i].description = FormatUtilities.truncate(data[i].description, 50);        data[i].cProjectId = FormatUtilities.truncate(data[i].cProjectId, 14);        data[i].cCampaignId = FormatUtilities.truncate(data[i].cCampaignId, 14);        data[i].cActivityId = FormatUtilities.truncate(data[i].cActivityId, 12);        data[i].user1Id = FormatUtilities.truncate(data[i].user1Id, 40);        data[i].user2Id = FormatUtilities.truncate(data[i].user2Id, 40);}
    }
    return data;
  }



    void refreshSessionEdit(VariablesSecureApp vars, FieldProvider[] data) {
      if (data==null || data.length==0) return;
          vars.setSessionValue(windowId + "|AD_Client_ID", data[0].getField("adClientId"));    vars.setSessionValue(windowId + "|AD_Org_ID", data[0].getField("adOrgId"));    vars.setSessionValue(windowId + "|C_Settlement_ID", data[0].getField("cSettlementId"));
    }

    void refreshSessionNew(VariablesSecureApp vars) throws IOException,ServletException {
      ManualSettlementData[] data = ManualSettlementData.selectEdit(this, vars.getLanguage(), vars.getStringParameter("inpcSettlementId", ""));
      if (data==null || data.length==0) return;
      refreshSessionEdit(vars, data);
    }

  String nextElement(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (strSelected == null || strSelected.equals("")) return firstElement(vars);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting next element");
        e.printStackTrace();
      }
      if (data!=null) {
        boolean found = false;
        for (int i=0;i<data.length;i++) {
          if (!found && data[i].getField("ID").equals(strSelected)) found=true;
          else if (found) return data[i].getField("ID");
        }
      }
    }
    return strSelected;
  }

  int getKeyPosition(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("getKeyPosition: " + strSelected);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting key position");
        e.printStackTrace();
      }
      if (data!=null) {
        for (int i=0;i<data.length;i++) {
          if (log4j.isDebugEnabled()) log4j.debug("row: " + i + " - key: " + data[i].getField("ID"));
          if (data[i].getField("ID").equals(strSelected)) return i;
        }
      }
    }
    return 0;
  }

  String previousElement(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (strSelected == null || strSelected.equals("")) return firstElement(vars);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting previous element");
        e.printStackTrace();
      }
      if (data!=null) {
        String previous = strSelected;
        for (int i=0;i<data.length;i++) {
          if (data[i].getField("ID").equals(strSelected)) return previous;
          previous = data[i].getField("ID");
        }
      }
    }
    return strSelected;
  }

  String firstElement(VariablesSecureApp vars) throws IOException, ServletException {
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,1);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting first element");
        e.printStackTrace();
      }
      if (data!=null && data.length!=0) return data[0].getField("ID");
    }
    return "";
  }

  String lastElement(VariablesSecureApp vars) throws IOException, ServletException {
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting last element");
        e.printStackTrace();
      }
      if (data!=null && data.length!=0) return data[data.length-1].getField("ID");
    }
    return "";
  }

  void printPageDataSheet(HttpServletResponse response, VariablesSecureApp vars, String strC_Settlement_ID)
    throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("Output: dataSheet");

    String strParamDocumentNo = vars.getSessionValue(tabId + "|paramDocumentNo");
String strParamDateTrx = vars.getSessionValue(tabId + "|paramDateTrx");
String strParamSettlementtype = vars.getSessionValue(tabId + "|paramSettlementtype");
String strParamDateTrx_f = vars.getSessionValue(tabId + "|paramDateTrx_f");

    boolean hasSearchCondition=false;
    vars.removeEditionData(tabId);
    if (!(strParamDocumentNo.equals("") && strParamDateTrx.equals("") && strParamSettlementtype.equals("") && strParamDateTrx_f.equals(""))) hasSearchCondition=true;
    String strOffset = vars.getSessionValue(tabId + "|offset");
    String selectedRow = "0";
    if (!strC_Settlement_ID.equals("")) {
      selectedRow = Integer.toString(getKeyPosition(vars, strC_Settlement_ID));
    }

    String[] discard={"isNotFiltered","isNotTest"};
    if (hasSearchCondition) discard[0] = new String("isFiltered");
    if (vars.getSessionValue("#ShowTest", "N").equals("Y")) discard[1] = new String("isTest");
    XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpWindows/ManualSettlement/ManualSettlement_Relation", discard).createXmlDocument();

    ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ManualSettlement", false, "document.frmMain.inpcSettlementId", "grid", "..", "".equals("Y"), "ManualSettlement", strReplaceWith, false);
    toolbar.prepareRelationTemplate("N".equals("Y"), hasSearchCondition, vars.getSessionValue("#ShowTest", "N").equals("Y"), "N".equals("Y"));
    xmlDocument.setParameter("toolbar", toolbar.toString());



    String strOrderBy = vars.getSessionValue(tabId + "|orderby");
    StringBuffer orderByArray = new StringBuffer();
    if (!strOrderBy.equals("")) {
      vars.setSessionValue(tabId + "|newOrder", "1");
      String positions = vars.getSessionValue(tabId + "|orderbyPositions");
      orderByArray.append("var orderByPositions = new Array(\n");
      if (!positions.equals("")) {
        StringTokenizer tokens=new StringTokenizer(positions, ",");
        boolean firstOrder = true;
        while(tokens.hasMoreTokens()){
          if (!firstOrder) orderByArray.append(",\n");
          orderByArray.append("\"").append(tokens.nextToken()).append("\"");
          firstOrder = false;
        }
      }
      orderByArray.append(");\n");
      String directions = vars.getSessionValue(tabId + "|orderbyDirections");
      orderByArray.append("var orderByDirections = new Array(\n");
      if (!positions.equals("")) {
        StringTokenizer tokens=new StringTokenizer(directions, ",");
        boolean firstOrder = true;
        while(tokens.hasMoreTokens()){
          if (!firstOrder) orderByArray.append(",\n");
          orderByArray.append("\"").append(tokens.nextToken()).append("\"");
          firstOrder = false;
        }
      }
      orderByArray.append(");\n");
    }

    xmlDocument.setParameter("selectedColumn", "\nvar selectedRow = " + selectedRow + ";\n" + orderByArray.toString());
    xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
    xmlDocument.setParameter("windowId", windowId);
    xmlDocument.setParameter("KeyName", "cSettlementId");
    xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
    xmlDocument.setParameter("theme", vars.getTheme());
    //xmlDocument.setParameter("buttonReference", Utility.messageBD(this, "Reference", vars.getLanguage()));
    try {
      WindowTabs tabs = new WindowTabs(this, vars, tabId, windowId);
      xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
      xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
      xmlDocument.setParameter("childTabContainer", tabs.childTabs());
      NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ManualSettlement_Relation.html", "ManualSettlement", "W", strReplaceWith, tabs.breadcrumb());
      xmlDocument.setParameter("navigationBar", nav.toString());
      LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ManualSettlement_Relation.html", strReplaceWith);
      xmlDocument.setParameter("leftTabs", lBar.relationTemplate());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    try {
      KeyMap key = new KeyMap(this, vars, tabId, windowId);
      xmlDocument.setParameter("keyMap", key.getRelationKeyMaps());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    {
      OBError myMessage = vars.getMessage(tabId);
      vars.removeMessage(tabId);
      if (myMessage!=null) {
        xmlDocument.setParameter("messageType", myMessage.getType());
        xmlDocument.setParameter("messageTitle", myMessage.getTitle());
        xmlDocument.setParameter("messageMessage", myMessage.getMessage());
      }
    }


    xmlDocument.setParameter("grid", Utility.getContext(this, vars, "#RecordRange", windowId));

    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println(xmlDocument.print());
    out.close();
  }

  void printPageEdit(HttpServletResponse response, HttpServletRequest request, VariablesSecureApp vars,boolean boolNew, String strC_Settlement_ID)
    throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("Output: edit");
    String strOrderByFilter = vars.getSessionValue(tabId + "|orderby");
    String orderClause = " C_Settlement.DocumentNo DESC";
    if (strOrderByFilter==null || strOrderByFilter.equals("")) strOrderByFilter = orderClause;
    /*{
      if (!strOrderByFilter.equals("") && !orderClause.equals("")) strOrderByFilter += ", ";
      strOrderByFilter += orderClause;
    }*/
    String strCommand = null;
    ManualSettlementData[] data=null;
    XmlDocument xmlDocument=null;
    FieldProvider dataField = vars.getEditionData(tabId);
    vars.removeEditionData(tabId);
    String strParamDocumentNo = vars.getSessionValue(tabId + "|paramDocumentNo");
String strParamDateTrx = vars.getSessionValue(tabId + "|paramDateTrx");
String strParamSettlementtype = vars.getSessionValue(tabId + "|paramSettlementtype");
String strParamDateTrx_f = vars.getSessionValue(tabId + "|paramDateTrx_f");

    
    String strParamTransaccional = vars.getStringParameter("inpParamSessionDate", Utility.getTransactionalDate(this, vars, windowId));
    if (!(strParamDocumentNo.equals("") && strParamDateTrx.equals("") && strParamSettlementtype.equals("") && strParamDateTrx_f.equals(""))) strParamTransaccional = "";
    
    
    boolean hasSearchCondition=false;
    if (!(strParamDocumentNo.equals("") && strParamDateTrx.equals("") && strParamSettlementtype.equals("") && strParamDateTrx_f.equals(""))) hasSearchCondition=true;

       String strParamSessionDate = vars.getGlobalVariable("inpParamSessionDate", Utility.getTransactionalDate(this, vars, windowId), "");
      String strParamUser = vars.getUser();
      String buscador = "";
      String[] discard = {"", "isNotTest"};
      
      if (vars.getSessionValue("#ShowTest", "N").equals("Y")) discard[1] = new String("isTest");
    if (dataField==null) {
      if (!boolNew) {
        discard[0] = new String("newDiscard");
        data = ManualSettlementData.selectEdit(this, vars.getLanguage(), strC_Settlement_ID);
  
        refreshSessionEdit(vars, data);
        strCommand = "EDIT";
      }

      if (boolNew || data==null || data.length==0) {
        discard[0] = new String ("editDiscard");
        strCommand = "NEW";
        data = new ManualSettlementData[0];
      } else {
        discard[0] = new String ("newDiscard");
      }
    } else {
      if (dataField.getField("cSettlementId").equals("")) {
        discard[0] = new String ("editDiscard");
        strCommand = "NEW";
        boolNew = true;
      } else {
        discard[0] = new String ("newDiscard");
        strCommand = "EDIT";
      }
    }
    xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpWindows/ManualSettlement/ManualSettlement_Edition",discard).createXmlDocument();

    ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ManualSettlement", (boolNew || (dataField==null && (data==null || data.length==0))), "document.frmMain.inpcSettlementId", "", "..", "".equals("Y"), "ManualSettlement", strReplaceWith, true);
    toolbar.prepareEditionTemplate("N".equals("Y"), hasSearchCondition, vars.getSessionValue("#ShowTest", "N").equals("Y"), "N".equals("Y"));
    xmlDocument.setParameter("toolbar", toolbar.toString());

    try {
      WindowTabs tabs = new WindowTabs(this, vars, tabId, windowId);
      xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
      xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
      xmlDocument.setParameter("childTabContainer", tabs.childTabs());
      NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ManualSettlement_Relation.html", "ManualSettlement", "W", strReplaceWith, tabs.breadcrumb());
      xmlDocument.setParameter("navigationBar", nav.toString());
      LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ManualSettlement_Relation.html", strReplaceWith);
      xmlDocument.setParameter("leftTabs", lBar.editionTemplate());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    try {
      KeyMap key = new KeyMap(this, vars, tabId, windowId);
      xmlDocument.setParameter("keyMap", key.getEditionKeyMaps(boolNew));
    } catch (Exception ex) {
      throw new ServletException(ex);
    }

        String strTYPESETTLEMENT = "I";
    vars.setSessionValue(windowId + "|TYPESETTLEMENT", strTYPESETTLEMENT);
        String strDOCTYPE_SETTLEMENT = "STM";
    vars.setSessionValue(windowId + "|DOCTYPE_SETTLEMENT", strDOCTYPE_SETTLEMENT);
    

    if (dataField==null) {
      if (boolNew || data==null || data.length==0) {
        refreshSessionNew(vars);
        data = ManualSettlementData.set(Utility.getDefault(this, vars, "AD_Client_ID", "@AD_CLIENT_ID@", "800006", ""), Utility.getDefault(this, vars, "AD_Org_ID", "@#AD_ORG_ID@", "800006", ""), Utility.getDefault(this, vars, "DocumentNo", "", "800006", ""), Utility.getDefault(this, vars, "C_Currency_ID", "", "800006", ""), ManualSettlementData.selectDef800335(this, Utility.getContext(this, vars, "AD_Org_ID", "800006"), Utility.getContext(this, vars, "#AD_Client_ID", windowId), Utility.getContext(this, vars, "DOCTYPE_SETTLEMENT", "800006")), Utility.getDefault(this, vars, "Settlementtype", "@TYPESETTLEMENT@", "800006", ""), Utility.getDefault(this, vars, "DateTrx", "@DATE@", "800006", ""), Utility.getDefault(this, vars, "DateAcct", "@DATE@", "800006", ""), Utility.getDefault(this, vars, "Description", "", "800006", ""), Utility.getDefault(this, vars, "IsTemplate", "N", "800006", "N"), Utility.getDefault(this, vars, "C_Project_ID", "", "800006", ""), Utility.getDefault(this, vars, "C_Campaign_ID", "", "800006", ""), Utility.getDefault(this, vars, "C_Activity_ID", "", "800006", ""), Utility.getDefault(this, vars, "Generatedamt", "0", "800006", ""), Utility.getDefault(this, vars, "User1_ID", "", "800006", ""), Utility.getDefault(this, vars, "User2_ID", "", "800006", ""), Utility.getDefault(this, vars, "CopyFrom", "N", "800006", "N"), Utility.getDefault(this, vars, "Processed", "N", "800006", "N"), (vars.getLanguage().equals("en_US")?ListData.selectName(this, "800024", Utility.getDefault(this, vars, "Processed", "N", "800006", "N")):ListData.selectNameTrl(this, vars.getLanguage(), "800024", Utility.getDefault(this, vars, "Processed", "N", "800006", "N"))), Utility.getDefault(this, vars, "Posted", "N", "800006", "N"), (vars.getLanguage().equals("en_US")?ListData.selectName(this, "234", Utility.getDefault(this, vars, "Posted", "N", "800006", "N")):ListData.selectNameTrl(this, vars.getLanguage(), "234", Utility.getDefault(this, vars, "Posted", "N", "800006", "N"))), "Y", Utility.getDefault(this, vars, "Processing", "", "800006", "N"), Utility.getDefault(this, vars, "SettlementOpen", "N", "800006", "N"), Utility.getDefault(this, vars, "Cancelednotchargeamt", "0", "800006", ""), Utility.getDefault(this, vars, "C_Settlement_ID", "", "800006", "0"), Utility.getDefault(this, vars, "Chargedamt", "0", "800006", ""), Utility.getDefault(this, vars, "CreateFile", "N", "800006", "N"), Utility.getDefault(this, vars, "CreateFrom", "", "800006", "N"));
             data[0].documentno = "<" + Utility.getDocumentNo(this, vars, windowId, "C_Settlement", "", data[0].cDoctypeId, false, false) + ">";
      }
    }

        xmlDocument.setParameter("TYPESETTLEMENT", strTYPESETTLEMENT);
        xmlDocument.setParameter("DOCTYPE_SETTLEMENT", strDOCTYPE_SETTLEMENT);
    

    xmlDocument.setParameter("commandType", strCommand);
    xmlDocument.setParameter("buscador",buscador);
    xmlDocument.setParameter("windowId", windowId);
    xmlDocument.setParameter("changed", (strCommand.equals("NEW")?"1":""));
    xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
    xmlDocument.setParameter("theme", vars.getTheme());
    //xmlDocument.setParameter("buttonReference", Utility.messageBD(this, "Reference", vars.getLanguage()));

    xmlDocument.setParameter("paramSessionDate", strParamSessionDate);

    xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
    {
      OBError myMessage = vars.getMessage(tabId);
      vars.removeMessage(tabId);
      if (myMessage!=null) {
        xmlDocument.setParameter("messageType", myMessage.getType());
        xmlDocument.setParameter("messageTitle", myMessage.getTitle());
        xmlDocument.setParameter("messageMessage", myMessage.getMessage());
      }
    }
    xmlDocument.setParameter("displayLogic", getDisplayLogicContext(vars));
    
    if (dataField==null) xmlDocument.setData("structure1",data);
    else {
      FieldProvider[] dataAux = new FieldProvider[1];
      dataAux[0] = dataField;
      xmlDocument.setData("structure1",dataAux);
    }
    try {
      ComboTableData comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "AD_Client_ID", "", "129", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("adClientId"):dataField.getField("adClientId")));
xmlDocument.setData("reportAD_Client_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "AD_Org_ID", "", "104", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("adOrgId"):dataField.getField("adOrgId")));
xmlDocument.setData("reportAD_Org_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_Currency_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cCurrencyId"):dataField.getField("cCurrencyId")));
xmlDocument.setData("reportC_Currency_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_DocType_ID", "", "149", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cDoctypeId"):dataField.getField("cDoctypeId")));
xmlDocument.setData("reportC_DocType_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "17", "Settlementtype", "800010", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("settlementtype"):dataField.getField("settlementtype")));
xmlDocument.setData("reportSettlementtype","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
xmlDocument.setParameter("DateTrx_Format", vars.getSessionValue("#AD_SqlDateFormat"));
xmlDocument.setParameter("DateAcct_Format", vars.getSessionValue("#AD_SqlDateFormat"));
comboTableData = new ComboTableData(vars, this, "19", "C_Project_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cProjectId"):dataField.getField("cProjectId")));
xmlDocument.setData("reportC_Project_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_Campaign_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cCampaignId"):dataField.getField("cCampaignId")));
xmlDocument.setData("reportC_Campaign_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_Activity_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cActivityId"):dataField.getField("cActivityId")));
xmlDocument.setData("reportC_Activity_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
xmlDocument.setParameter("buttonGeneratedamt", Utility.messageBD(this, "Calc", vars.getLanguage()));
comboTableData = new ComboTableData(vars, this, "18", "User1_ID", "134", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("user1Id"):dataField.getField("user1Id")));
xmlDocument.setData("reportUser1_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "18", "User2_ID", "137", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("user2Id"):dataField.getField("user2Id")));
xmlDocument.setData("reportUser2_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
xmlDocument.setParameter("buttonChargedamt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonCancelednotchargeamt", Utility.messageBD(this, "Calc", vars.getLanguage()));

    } catch (Exception ex) {
      ex.printStackTrace();
      throw new ServletException(ex);
    }
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println(xmlDocument.print());
    out.close();
  }

    void printPageButtonProcessed800025(HttpServletResponse response, VariablesSecureApp vars, String strC_Settlement_ID, String strprocessed, String strProcessing)
    throws IOException, ServletException {
      log4j.debug("Output: Button process 800025");
      String[] discard = {"newDiscard"};
      response.setContentType("text/html; charset=UTF-8");
      PrintWriter out = response.getWriter();
      XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/Processed800025", discard).createXmlDocument();
      xmlDocument.setParameter("key", strC_Settlement_ID);
      xmlDocument.setParameter("processing", strProcessing);
      xmlDocument.setParameter("form", "ManualSettlement_Edition.html");
      xmlDocument.setParameter("window", windowId);
      xmlDocument.setParameter("css", vars.getTheme());
      xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
      xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
      xmlDocument.setParameter("cancel", Utility.messageBD(this, "Cancel", vars.getLanguage()));
      xmlDocument.setParameter("ok", Utility.messageBD(this, "OK", vars.getLanguage()));
      
      try {
        KeyMap key = new KeyMap(this, vars, tabId, windowId);
        xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
      } catch (Exception ex) {
        throw new ServletException(ex);
      }
      
      {
        OBError myMessage = vars.getMessage("800025");
        vars.removeMessage("800025");
        if (myMessage!=null) {
          xmlDocument.setParameter("messageType", myMessage.getType());
          xmlDocument.setParameter("messageTitle", myMessage.getTitle());
          xmlDocument.setParameter("messageMessage", myMessage.getMessage());
        }
      }

          try {
    ComboTableData comboTableData = null;
    } catch (Exception ex) {
      throw new ServletException(ex);
    }

      out.println(xmlDocument.print());
      out.close();
    }


    String getDisplayLogicContext(VariablesSecureApp vars) throws IOException, ServletException {
      log4j.debug("Output: Display logic context fields");
      String result = "var str$Element_PJ=\"" + Utility.getContext(this, vars, "$Element_PJ", windowId) + "\";\nvar str$Element_MC=\"" + Utility.getContext(this, vars, "$Element_MC", windowId) + "\";\nvar str$Element_AY=\"" + Utility.getContext(this, vars, "$Element_AY", windowId) + "\";\nvar str$Element_U1=\"" + Utility.getContext(this, vars, "$Element_U1", windowId) + "\";\nvar str$Element_U2=\"" + Utility.getContext(this, vars, "$Element_U2", windowId) + "\";\nvar strShowAcct=\"" + Utility.getContext(this, vars, "ShowAcct", windowId) + "\";\n";
      return result;
    }




  public String getServletInfo() {
    return "Servlet ManualSettlement. This Servlet was made by Wad constructor";
  } // fin del método getServletInfo()
}
